package com.example.library.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data // 自动生成get set方法
@NoArgsConstructor // 无参构造方法
@AllArgsConstructor // 全参构造方法
public class Result<T> {

    // Status code constants
    public static final int SUCCESS_CODE = 0;
    public static final int ERROR_CODE = 1;

    private Integer code;    // Business status code: 0 - success, 1 - failure
    private String message;  // Message
    private T data;          // Response data

    // Constructors, getters, and setters...

    // Quick success response with data
    public static <E> Result<E> success(E data) {
        return new Result<>(SUCCESS_CODE, "Operation successful", data);
    }

    // Quick success response without data
    public static <E> Result<E> success() {
        return new Result<>(SUCCESS_CODE, "Operation successful", null);
    }

    // Quick error response with message
    public static <E> Result<E> error(String message) {
        return new Result<>(ERROR_CODE, message, null);
    }

    // Quick error response with code and message
    public static <E> Result<E> error(int code, String message) {
        return new Result<>(code, message, null);
    }

    // Quick error response with code, message, and data
    public static <E> Result<E> error(int code, String message, E data) {
        return new Result<>(code, message, data);
    }

    // New method to check if the operation was successful
    public boolean isSuccess() {
        return this.code.equals(SUCCESS_CODE);
    }
}

